<script>

import BtnSearchInput from "@/components/BtnSearchInput.vue"

export default {
  name: "Search",
  components: {BtnSearchInput},
  data() {
    return {
      searchInput: '',
    }
  },
  computed: {
    history() {
      return this.$store.getters.globalHistory
    }
  },
  methods: {
    // key是输入框内容或者点击的历史记录
    goSearch(key) {
      // 输入框不能为空
      if (!key.trim()) return uni.showToast({icon: 'none', title: '输入不得为空'})

      // 跳转到搜索列表页
      uni.navigateTo({url: `/pages/SearchList/SearchList?search=${key}`})
    },
    clearHistory() {
      this.$store.commit('user/REMOVE_HISTORY') // 把本地存储的历史记录也清空
    },
  },
}
</script>

<template>
  <div class="search">
    <van-nav-bar title="商品搜索" left-arrow @click-left="uni.navigateBack"/>

    <!--搜索框-->
    <!-- btnFun 方法传入的参数是 btnFunParams ,是一个数组,而且 v-model 必传，这样才可以绑定数据和 input 框-->
    <BtnSearchInput @btn-fun="goSearch" v-model="searchInput"></BtnSearchInput>

    <!-- 搜索历史 -->
    <div class="search-history">
      <div class="title">
        <span>最近搜索</span>
        <van-icon @click="clearHistory" name="delete-o" size="16"/>
      </div>
      <div class="list">
        <div v-for="item in history" :key="item" class="list-item" @click="goSearch(item)">{{ item }}</div>
      </div>
    </div>

  </div>
</template>

<style lang="less">
.search {

  .title {
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 15px;
  }

  .list {
    display: flex;
    flex-wrap: wrap;
    padding: 0 10px;
    gap: 5%;
  }

  .list-item {
    width: 30%;
    text-align: center;
    padding: 7px;
    line-height: 15px;
    border-radius: 50px;
    background-color: #fff;
    font-size: 13px;
    border: 1px solid #efefef;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    margin-bottom: 10px;
  }
}

</style>
